﻿using Microsoft.IdentityModel.Tokens;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Cl.Utils.Extention
{
    public static class Extensions
    {
        public static bool ToLimsBool(this string value)
        {
            try
            {

                if (value.IsNullOrEmpty())
                {
                    return false;
                }
                else
                {
                    bool.TryParse(value, out bool b);
                    return b;
                }

            }
            catch (Exception ex)
            {
                return false;
            }
        }


        public static List<string> ToLimsList(this string value)
        {
            try
            {

                if (value.IsNullOrEmpty())
                {
                    return new List<string>();
                }
                else
                {

                    string[] result = value.Split(',');


                    return result.ToList();
                }

            }
            catch (Exception ex)
            {
                return new List<string>();
            }
        }


        public static int ToLimsInt32(this string value)
        {
            try
            {

                if (value.IsNullOrEmpty())
                {
                    return 0;
                }
                else
                {
                    int.TryParse(value, out int b);
                    return b;
                }

            }
            catch (Exception ex)
            {
                return 0;
            }
        }

        public static DateTime? ToLimsDateTime(this string value)
        {
            try
            {

                if (value.IsNullOrEmpty())
                {
                    return null;
                }
                else
                {
                    DateTime.TryParse(value, out DateTime b);
                    return b;
                }

            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public static List<int> ToListIntIds(this string IdsStr)
        {
            try
            {
                var Ids = IdsStr.Split(',');
                var parsedIds = new List<int>();
                foreach (var orderId in Ids)
                {
                    if (int.TryParse(orderId.Trim(), out int id))
                    {
                        parsedIds.Add(id);
                    }
                }
                return parsedIds;
            }
            catch (Exception)
            {

                return new();
            }
        }

    }
}
